/*
   This file is part of kleber.
   Copyright 2008 Simon Schmeisser <mail_to_wrt@gmx.de>
   
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
    
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "KleberPage.h"

#include "zebra.h"

#include <iostream>

#include <cstdlib>

#include <QtGui/QWidget>

KleberPage::KleberPage(const QString &fileName)
{
	m_fileName = fileName;
	m_imageLoaded = false;
	m_scanned = false;
}

void KleberPage::registerClass(QScriptEngine* engine)
{
	if (!engine)
		return ;
	
	QScriptValue ctor = engine->newFunction(KleberPageConstructor);
	QScriptValue metaObject = engine->newQMetaObject(&QObject::staticMetaObject, ctor);
	engine->globalObject().setProperty("Page", metaObject);
}

bool KleberPage::loadImage()
{
	if ( m_imageLoaded )
		return true;

	m_imageLoaded = m_image.load(m_fileName);

	if ( not m_imageLoaded ) {
		//ERROR: File does not exist
		return false;
	}

	m_pixelWidth = m_image.width();
	m_pixelHeight = m_image.height();

	m_resolution = m_image.dotsPerMeterX();

	return true;
}

QImage KleberPage::image()
{
	loadImage();
	
	return m_image;
}

void KleberPage::scanBarcodes()
{
	if ( m_scanned )
		return ;
	
	loadImage();
	
	m_barcodes.clear();
	
	//return ;
	
	//zebra::zebra_set_verbosity(10);
	
	int width = m_image.width();
	int height = m_image.height();
	
	zebra::Image zimage(width, height, "RGB4",
                 m_image.bits(), width*height*4);
	//std::cout << "numBytes(): " << m_image.numBytes() << std::endl;
	
	zebra::ImageScanner scanner;
	scanner.scan(zimage);
	
	//std::cout << "Scanning for barcodes ... " << std::endl;
	
	for(zebra::Image::SymbolIterator sym = zimage.symbol_begin();
		sym != zimage.symbol_end();
		++sym)
	{
		std::cout << (*sym).get_data() << std::endl;
		//m_numBarcodes++;
		QString data((*sym).get_data().c_str());
		
		int value;
		bool ok;
		value = data.toInt(&ok);
		if (ok) {
			m_barcodes.append(value);
		}
	}
	std::cout.flush();
	
	m_scanned = true;
}

int KleberPage::numBarcodes()
{
	//scan the stuff
	scanBarcodes();
	
	return m_barcodes.size();
}

int KleberPage::barcode( int pos )
{
	//scan the stuff
	scanBarcodes();
	
	return m_barcodes.value( pos, -1 );
}

int KleberPage::pixelWidth()
{
	loadImage();

	return m_pixelWidth;
}

int KleberPage::pixelHeight()
{
	loadImage();

	return m_pixelHeight;
}

int KleberPage::resolution()
{
	loadImage();

	return m_resolution;
}

QScriptValue KleberPageConstructor(QScriptContext * context, QScriptEngine * engine)
{
	QString fileName = context->argument(0).toString();
	//QObject *parent = context->argument(1).toQObject();
	KleberPage *page = new KleberPage(fileName);//, parent);
	return engine->newQObject(page, QScriptEngine::ScriptOwnership);
}

